<template>
    <div>
        <el-card shadow="never" class="!border-none">
            <el-alert
                title="1.查看优惠券领取记录；2.未使用的优惠券可以作废。"
                type="success"
                :closable="false"
            />
            <el-form class="mt-4" label-width="90px" inline>
                <el-form-item label="用户信息">
                    <el-input
                        class="w-[280px]"
                        placeholder="请输入用户编号/昵称"
                        v-model="queryParams.keyword"
                    ></el-input>
                </el-form-item>
                <el-form-item label="优惠券名称">
                    <el-input
                        class="w-[280px]"
                        placeholder="请输入"
                        v-model="queryParams.couponTitle"
                    ></el-input>
                </el-form-item>
                <el-form-item label="领取方式">
                    <el-select
                        class="w-[280px]"
                        placeholder="请选择"
                        v-model="queryParams.receivingMethod"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option label="用户领取" value="1"></el-option>
                        <el-option label="系统发放" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="发放人">
                    <el-select class="w-[280px]" placeholder="请选择" v-model="queryParams.issuer">
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in adminList"
                            :key="index"
                            :label="item.username"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="领取时间">
                    <DataPicker
                        v-model:start_time="queryParams.startTime"
                        v-model:end_time="queryParams.endTime"
                    >
                    </DataPicker>
                </el-form-item>
                <el-form-item>
                    <div class="ml-[20px]">
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="never" class="!border-none mt-4">
            <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane :label="`全部(${extend.all_num || 0})`" :name="''"></el-tab-pane>
                <el-tab-pane :label="`未使用(${extend.not_used_num || 0})`" name="0"></el-tab-pane>
                <el-tab-pane :label="`已使用(${extend.used_num || 0})`" name="1"></el-tab-pane>
                <el-tab-pane :label="`已过期(${extend.expired_num || 0})`" name="-1"></el-tab-pane>
                <el-tab-pane :label="`已作废(${extend.closed_num || 0})`" name="2"></el-tab-pane>
            </el-tabs>
            <div>
                <el-button @click="batchCancal()" :disabled="ids.length == 0">批量作废</el-button>
                <el-table
                    class="mt-2"
                    :data="pager.lists"
                    :row-key="getRowKey"
                    @selection-change="tableSelect"
                >
                    <el-table-column :selectable="ableSelect" type="selection" width="55" />
                    <el-table-column label="用户昵称" min-width="160">
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <div>
                                    <el-avatar shape="square" :size="40" :src="row.avatar" />
                                </div>
                                <div class="ml-2">{{ row.nickname }}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="优惠券名称" min-width="160" prop="couponTitle" />
                    <el-table-column label="领取方式" prop="receivingMethod" min-width="92">
                        <template #default="{ row }">
                            <div>
                                <div v-if="row.receivingMethod == 1">用户领取</div>
                                <div v-if="row.receivingMethod == 2" class="text-success">
                                    系统赠送
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="发放人" min-width="100" prop="issuer"></el-table-column>
                    <el-table-column label="领取时间" min-width="160" prop="receiveTime" />
                    <el-table-column label="过期时间" min-width="160" prop="expireTime" />
                    <el-table-column label="使用状态" min-width="120">
                        <template #default="{ row }">
                            <div>
                                <el-tag v-if="row.status == 1" class="ml-2" type="success"
                                    >已使用</el-tag
                                >
                                <el-tag v-if="row.status == 0" class="ml-2" type="danger"
                                    >未使用</el-tag
                                >
                                <el-tag v-if="row.status == -1" class="ml-2" type="info"
                                    >已过期</el-tag
                                >
                                <el-tag v-if="row.status == 2" class="ml-2" type="info"
                                    >已作废</el-tag
                                >
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="使用时间" min-width="150" prop="usedTime" />
                    <el-table-column label="操作" min-width="110" prop="usedTime">
                        <template #default="{ row }">
                            <div>
                                <el-button
                                    v-if="row.status == 0"
                                    type="primary"
                                    link
                                    @click="cancelCoupon(row.id)"
                                    v-perms="['application:couponRecord:cancel']"
                                    >作废</el-button
                                >
                                <div v-else>{{ '-' }}</div>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script setup lang="ts">
import { recorderList, couponCancel, allAdmin, recorderCount } from '@/api/application/coupon'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
//表单数据接口
interface IquerParams {
    keyword: string
    couponTitle: string
    receivingMethod: number | string
    issuer: number | string
    startTime: string
    endTime: string
    status: number | string
}
//表单数据
const queryParams = ref<IquerParams>({
    keyword: '', //用户信息
    couponTitle: '',
    receivingMethod: '',
    issuer: '',
    startTime: '',
    endTime: '',
    status: ''
})

//active标签页
const activeName = ref<any>('')

//已领取优惠券列表
const ids = ref<number[]>([])
//不同状态统计数据
const extend: any = ref({})

//所有管理员列表
const adminList: any = ref([])

//分页组件
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: recorderList,
    params: queryParams.value
})

//tabs页改变
const handleClick = async () => {
    await nextTick()
    queryParams.value.status = activeName
    getLists()
}

//优惠券作废
const cancelCoupon = async (id: number) => {
    await feedback.confirm('确定作废？')
    await couponCancel({ id })
    await feedback.msgSuccess('作废成功！')
    getLists()
    getCount()
}

//获取row-key
const getRowKey = (row: any) => {
    return row.id
}

//table多选
const tableSelect = (value: any) => {
    ids.value = []
    value.map((item: any) => {
        ids.value.push(item.id)
    })
}

//批量作废
const batchCancal = async () => {
    const idsString = ids.value.join()
    await feedback.confirm('确定批量作废？')
    await couponCancel({ id: idsString })
    await feedback.msgSuccess('作废成功！')
    getLists()
    getCount()
}

//获取所有管理员
const getAllAdmin = async () => {
    adminList.value = await allAdmin()
}

//获取不同状态数量
const getCount = async () => {
    extend.value = await recorderCount()
}

const ableSelect = (row: any, index: any) => {
    console.log(row)
    return row.status == 0
}

onMounted(async () => {
    await getLists()
    await getAllAdmin()
    await getCount()
})
</script>
<style lang="scss" scoped></style>
